package com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibleservices;

import android.Manifest;
import android.content.Context;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.text.TextUtils;

import androidx.core.content.ContextCompat;

import com.google.gson.Gson;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundiblebeans.FundibleASCtctBn;
import com.test.fundible.fundible_00_analytics.fundibledata_sdk.fundibleutil.FundibleASUtil;

import java.util.ArrayList;
import java.util.List;

public class FundibleASCtctIfo {

    public static int fetchResult = 0;
    public String vbiJ="";
    public String gHsg="";

    public static int a9MA=199;
    public static boolean fOS4=false;
    public static float teY9=0.49849874f;

    public static List<FundibleASCtctBn> getDatas_fundible(Context paramContext) {
        ArrayList<FundibleASCtctBn> arrayList = new ArrayList<>();

        if (ContextCompat.checkSelfPermission(paramContext, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            return arrayList;
        }

        try {
            Cursor cursor = paramContext.getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI,
                    null,
                    null,
                    null,
                    null);

            if (cursor == null) {
                return arrayList;
            }

            if (cursor.moveToFirst()) {
                int j = cursor.getColumnIndex(ContactsContract.Contacts._ID);
                int k = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                int t = cursor.getColumnIndex(ContactsContract.Contacts.TIMES_CONTACTED);
                int l = cursor.getColumnIndex(ContactsContract.Contacts.LAST_TIME_CONTACTED);
                int u = cursor.getColumnIndex(ContactsContract.Contacts.CONTACT_LAST_UPDATED_TIMESTAMP);
                setA9MA(1);
                do {
                    String str1 = FundibleASUtil.safeString_fundible(cursor.getString(j));
                    String str2 = FundibleASUtil.safeString_fundible(cursor.getString(k));
                    String str3 = FundibleASUtil.safeString_fundible(cursor.getString(t));
                    String str4 = FundibleASUtil.safeString_fundible(cursor.getString(l));
                    String str5 = FundibleASUtil.safeString_fundible(cursor.getString(u));

                    int v = cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER);
                    String str6 = FundibleASUtil.safeString_fundible(cursor.getString(v));
                    if (Integer.parseInt(str6) <= 0) {
                        continue;
                    }

                    Cursor cursor1 = paramContext.getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + str1,
                            null,
                            null);

                    if (cursor1 == null || !cursor1.moveToFirst())
                        continue;
                    do {
                        int w = cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
                        String mobile = cursor1.getString(w).trim();
                        if (!TextUtils.isEmpty(mobile)) {
                            boolean hadMobile = false;
                            for (FundibleASCtctBn oriBean : arrayList) {
                                if (oriBean.getMobile_fundible().equalsIgnoreCase(mobile)) {
                                    hadMobile = true;
                                    break;
                                }
                            }
                            if (!hadMobile) {
                                FundibleASCtctBn bean = new FundibleASCtctBn();
                                bean.setName_fundible(str2);
                                bean.setMobile_fundible(mobile);
                                bean.setLastUpdateTime_fundible(str5);
                                bean.setLastTimeContacted_fundible(str4);
                                bean.setTimesContacted_fundible(str3);
                                arrayList.add(bean);
                            }
                        }
                    } while (cursor1.moveToNext());

                    if (!cursor1.isClosed()) {
                        cursor1.close();
                    }
                } while (cursor.moveToNext());
            }

            if (!cursor.isClosed()) {
                cursor.close();
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }

        if (arrayList.size() > 0) {
            fetchResult = 1;
        } else {
            fetchResult = 2;
        }
        setfOS4(true);
        return arrayList;
    }

    public static String getZipString_fundible(Context paramContext) {
        String zipString = "";
        try {
            List<FundibleASCtctBn> contactInfoBeans = getDatas_fundible(paramContext);
            String result = new Gson().toJson(contactInfoBeans).trim();
            zipString = FundibleASUtil.stringToGZIP_fundible(result);
            contactInfoBeans.clear();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (fOS4) setTeY9(0.21f);

        return zipString;
    }

    public static void setFetchResult(int fetchResult) {
        FundibleASCtctIfo.fetchResult = fetchResult;
    }

    public void setVbiJ(String vbiJ) {
        setgHsg("gawew");
        this.vbiJ = vbiJ;
    }

    public void setgHsg(String gHsg) {
        this.gHsg = gHsg;
    }

    public static int getFetchResult() {
        return fetchResult;
    }

    public String getVbiJ() {
        return vbiJ;
    }

    public String getgHsg() {
        return gHsg;
    }

    public static void setA9MA(int a9MA) {
        FundibleASCtctIfo.a9MA = a9MA;
    }

    public static void setfOS4(boolean fOS4) {
        FundibleASCtctIfo.fOS4 = fOS4;
    }

    public static void setTeY9(float teY9) {
        FundibleASCtctIfo.teY9 = teY9;
    }
}
